const rawArray = [1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 4, 5, 6, 3, NaN, NaN]

// 1、new Set()集合
let array2Set = [...new Set(rawArray)]
console.log('Set()方法 原数组不会被改变：', array2Set);

// 2、Array.from(一个类似数组或可迭代对象)和Set()
let arrayToSet = Array.from(new Set(rawArray))
console.log('Array.from()和Set() 原数组不会被改变：', array2Set);

// 3、for循环和indexOf()方法
let arrayForIndexOf = []
for (let i = 0; i < rawArray.length; i++) {
	if (arrayForIndexOf.indexOf(rawArray[i]) == -1) arrayForIndexOf.push(rawArray[i])
}
console.log('for循环和indexOf()方法 原数组不会被改变：', arrayForIndexOf);

// 4、for循环和includes()方法
let arrayForIncludes = []
for (let i = 0; i < rawArray.length; i++) {
	if (!arrayForIncludes.includes(rawArray[i])) {
		arrayForIncludes.push(rawArray[i])
	}
}
console.log('for循环和includes()方法 原数组不会被改变：', arrayForIndexOf);

// 5、for循环和new Map
let array2Map = new Map()
let res = []
for (let i = 0; i < rawArray.length; i++) {
	if (array2Map.has(rawArray[i])) {
		array2Map.set(rawArray[i], true)
	} else {
		array2Map.set(rawArray[i], false)
		res.push(rawArray[i])
	}
}
console.log('for循环和new Map() 原数组不会被改变：', res);

// 6、new Map和filter()方法
let map = new Map()
let res1 = rawArray.filter(item => !map.has(item) && map.set(item, 1))
console.log('new Map和filter()方法 原数组不会被改变', res1);

// 7、reduce()方法
let res2 = rawArray.reduce((accumulator, cur) => {
	return accumulator.includes(cur) ? accumulator : accumulator.concat(cur)
}, [])
console.log('reduce()方法 原数组不会被改变', res2);

// 8、对象键值对
let obj = {}
let res3 = []
for (let i = 0; i < rawArray.length; i++) {
	if (obj[rawArray[i]]) continue
	res3.push(rawArray[i])
	obj[rawArray[i]] = true
}
console.log('对象键值对 原数组不会被改变', res3);

// 9、双层for循环
const rawArray1 = [1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 4, 5, 6, 3, NaN, NaN]
for (let i = 0, len = rawArray1.length; i < len; i++) {
	for (let j = i + 1, len = rawArray1.length; j < len; j++) {
		if (rawArray1[i] == rawArray1[j]) {
			rawArray1.splice(j, 1)
			j--
			len--
		}
	}
}
console.log('双层for循环 原数组会改变：', rawArray1);

// 10、双层for循环
let arr = []
for (var i = 0, len = rawArray.length; i < len; i++) {
	for (var j = 0, arrLen = arr.length; j < arrLen; j++) {
		if (arr[i] === arr[j]) break
	}
	if (j === arrLen) {
		arr.push(arr[i])
	}
}
console.log('双层for循环 原数组不会改变：', rawArray1);

// 11、sort()
let rawArr = rawArray.sort((a, b) => a - b)
let pre
let arr1 = []
for (let i = 0, len = rawArr.length; i < len; i++) {
	if (!i || pre !== rawArr[i]) { // 判断是否为第一个元素或者相邻的元素不相同
		arr1.push(rawArr[i])
	}
	pre = rawArr[i]
}
console.log('for循环和sort() 原数组不会改变：', arr1);